package com.cxyd.modules.job.task.agvTask;

import com.cxyd.modules.api.dto.RcsResponseHK;
import com.cxyd.modules.wcs.dto.hikRcs.AgvSubmitDTO;
import com.cxyd.modules.wcs.dto.hikRcs.CarrierBindDTO;
import com.cxyd.modules.wcs.entity.TrkEntity;
import com.cxyd.modules.wcs.entity.UrlMappingEntity;
import com.cxyd.modules.wcs.service.AgvTrkHkService;
import com.cxyd.modules.wcs.service.TrkService;
import com.cxyd.modules.wcs.service.UrlMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Component("hk-task")
@Slf4j
public class HKTask {
    //    创建锁，防止并发情况出现
    private Lock lock = new ReentrantLock();
    //处理海康货架任务
    @Resource
    private TrkService trkService;
    //发送海康货架任务
    @Resource
    private AgvTrkHkService agvTrkHkService;
    @Resource
    private UrlMappingService urlService;
    @Resource
    @Qualifier("sslIgnoreRestTemplate")
    private RestTemplate sslIgnoreRestTemplate;

    /**
     * 载具与站点绑定
     */
    public void carrierBind() {
        // 创建一个绑定对象
        CarrierBindDTO carrierBind = new CarrierBindDTO();
        carrierBind.setCarrierCode("1");
        carrierBind.setSiteCode("0237133XY0320535");
        carrierBind.setExtra(null);
        // 获取URL配置
        UrlMappingEntity bindUrl = urlService.getUrlByNo("HK_BIND_URL");
        if (bindUrl == null || bindUrl.getUrlAdr().isEmpty()) {
            log.info("[HK_POST_URL]未配置!");
            return;
        }
        RcsResponseHK response = sslIgnoreRestTemplate.postForObject(bindUrl.getUrlAdr(), carrierBind, RcsResponseHK.class);
        if (response == null) {
            log.error("[HK_BIND_URL]无响应!");
            return;
        }
        log.info("获取海康反馈:code:{},msg:{},data:{}", response.getCode(), response.getMessage(), response.getData());
    }

    /**
     * 载具与站点解绑
     */
    public void carrierUnBind() {
        // 创建一个绑定对象
        CarrierBindDTO carrierUnBind = new CarrierBindDTO();
        carrierUnBind.setCarrierCode("1");
        carrierUnBind.setSiteCode("0237133XY0320535");
        carrierUnBind.setExtra(null);
        // 获取URL配置
        UrlMappingEntity bindUrl = urlService.getUrlByNo("HK_UNBIND_URL");
        if (bindUrl == null || bindUrl.getUrlAdr().isEmpty()) {
            log.info("[HK_UNBIND_URL]未配置!");
            return;
        }
        RcsResponseHK response = sslIgnoreRestTemplate.postForObject(bindUrl.getUrlAdr(), carrierUnBind, RcsResponseHK.class);
        if (response == null) {
            log.error("载具与站点解绑无响应!");
            return;
        }
        log.info("获取海康反馈:code:{},msg:{},data:{}", response.getCode(), response.getMessage(), response.getData());
    }

    public void scanTask() {
        lock.lock();
        try {
            final TrkEntity wcsTrk = trkService.selectByDvcNo("HK_AGV");
            if (wcsTrk == null) {
//                log.info("暂无[HK_AGV]搬送的任务!");
                return;
            }
            agvTrkHkService.hkTrkScan(wcsTrk);
        } catch (Exception e) {
            log.error("扫描[HK_AGV]任务异常，causeBy:{}", e.getMessage());
        } finally {
            lock.unlock();
        }
    }

    public void postTask() {
        lock.lock();
        try {
//            优先获取一下路由信息
            UrlMappingEntity url_hk = urlService.getUrlByNo("HK_POST_URL");
            if (url_hk == null || url_hk.getUrlAdr().isEmpty()) {
                log.info("[HK_POST_URL]未配置!");
                return;
            }
//            获取属于海康的[agv_trk_hk]搬送任务
            AgvSubmitDTO agvTrkHK = agvTrkHkService.getPostHkTrk();
            if (agvTrkHK == null) {
//                log.info("暂无[HK_AGV]的搬送任务!");
                return;
            }
            log.info("获取到待下发任务：{}", agvTrkHK);
            RcsResponseHK respond = sslIgnoreRestTemplate.postForObject(url_hk.getUrlAdr(), agvTrkHK, RcsResponseHK.class);
            if (respond == null) {
                return;
            }
            log.info("获取海康反馈:code:{},msg:{},data:{}", respond.getCode(), respond.getMessage(), respond.getData());
//            处理下发任务
//            agvTrkHkService.agvTrkHandle(agvTrkHK, respond);
        } catch (Exception e) {
            log.error("下发[HK_AGV]搬送任务异常，causeBy:{}", e.getMessage());
        } finally {
            lock.unlock();
        }
    }
}
